package com.gofun.report.util;

import java.util.Arrays;
import java.util.Date;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

public class App {
	  public static void main( String[] args )
	    {
	        // 创建MongoDB服务器地址对象
	        ServerAddress address = new ServerAddress("127.0.0.1", 27017);
	        // 创建MongoDB服务器用户验证对象
	        MongoCredential credential = MongoCredential.createCredential("xcoder",
	                "xcoder",
	                "123456".toCharArray());
	         
	        // 登陆到MongoDB服务器
	        MongoClient client = new MongoClient(address, Arrays.asList(credential));
	        // 获取存放在MongoDB服务器上指定的数据库
	        MongoDatabase db =  client.getDatabase("xcoder");
	        // 创建集合
	        db.createCollection("mycollection");
	        // 获取集合
	        MongoCollection<Document> collection = db.getCollection("mycollection");
	        // 创建一条数据
	        Document doc = new Document();
	        doc.append("_id", "500101198809112134");
	        doc.append("name", "XCoder Studio");
	        doc.append("age", 20);
	        doc.append("createTime", new Date());
	        // 将数据插入数据库
	        collection.insertOne(doc);
	        // 打印集合计数
	        System.out.println("Current Count: "+collection.count());
	        client.close();
	    }
}
